package com.zqxq.lottery.service.jczq.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.entity.bo.jczq.MatchApiBO;
import com.zqxq.lottery.entity.enums.LotteryIdEnum;
import com.zqxq.lottery.entity.po.jczq.JczqMatchPO;
import com.zqxq.lottery.entity.vo.jczq.*;
import com.zqxq.lottery.mapper.jczq.JczqMatchLiveMapper;
import com.zqxq.lottery.mapper.jczq.JczqMatchMapper;
import com.zqxq.lottery.service.jczq.IJczqMatchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
@Slf4j
public class JczqMatchServiceImpl extends ServiceImpl<JczqMatchMapper, JczqMatchPO> implements IJczqMatchService {

    @Autowired
    private JczqMatchLiveMapper matchLiveMapper;
    @Override
    public JczqMatchVO get(String id) throws Exception {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("code",id);
        JczqMatchPO po = this.selectOne(wrapper);
        JczqMatchVO vo = new JczqMatchVO();
        if(po!=null){
            vo.setCode(po.getCode());
            vo.setCloseTime(String.valueOf(po.getClose_time().getTime()));
            vo.setOdds(po.getOdds());
            vo.setMatchField(po.getMatch_field());
            vo.setHostTeam(po.getHost_team());
            vo.setGuestTeam(po.getGuest_team());
            vo.setScore(po.getScore());
            vo.setTotalScore(String.valueOf(po.getTotal_score()));
            vo.setDetailStatus(po.getDetail_status());
        }
        return vo;
    }

    @Override
    public List<LeagueApiVO> queryHotLeagueList() {
        return baseMapper.queryHotLeagueList();
    }

    //安卓赛事列表
    @Override
    public List<MatchApiVO1> queryHotMatchList(MatchApiBO search) {
        List<MatchApiVO1> list = new ArrayList<>();
        if (search == null || StrUtils.isEmpty(search.getMatchType()))
            search.setMatchType("0");
        List<String> codeList = baseMapper.getHotMatchCode(Integer.parseInt(search.getMatchType()));
        for(String code:codeList){
            search.setCode(code);
            MatchApiVO1 apiVO = getHotMatchApiByCode(search);
            if(null != apiVO)
                list.add(apiVO);
        }
        return list;
    }

    private MatchApiVO1 getHotMatchApiByCode(MatchApiBO search){
        List<MatchVO> matchList = baseMapper.getHotMatchList(search);
        if(null ==matchList||matchList.size()==0)
            return null;
        MatchApiVO1 matchApi = new MatchApiVO1(search.getCode());
        for(MatchVO match:matchList){
            match.setMatchField(match.getMatchField().substring(2));
            match.setOddsMap(JSONObject.parseObject(match.getOdds(), Map.class));
            if ("1".equals(search.getMatchType())) {
                //历史交锋
                if (StringUtils.isNotBlank(match.getOverallRecord())) {
                    JSONObject json = JSONObject.parseObject(match.getOverallRecord());
                    int win = json.getInteger("win");
                    int lose = json.getInteger("lose");
                    match.setOverallRecord(String.format("主队近%d场，%s%d胜%d负", win + lose, match.getHostTeam(), win, lose));
                }
                //近期战绩
                if (StringUtils.isNotBlank(match.getRecentRecord())) {
                    List<Map> jsonMap = JSONObject.parseArray(match.getRecentRecord(), Map.class);
                    String recentRecord = "";
                    for (int i = 0; i < jsonMap.size(); i++) {
                        Map json = jsonMap.get(i);
                        int win = (int) json.get("win");
                        int lose = (int) json.get("lose");
                        if (i == 0)
                            recentRecord += String.format("主队近%d场%d胜%d负",win + lose, win, lose);
                        if (i == 1)
                            recentRecord += String.format("，客队近%d场%d胜%d负",win + lose, win, lose);
                    }
                    match.setRecentRecord(recentRecord);
                }
            }else {
                //历史交锋
                if (StringUtils.isNotBlank(match.getOverallRecord())) {
                    JSONObject json = JSONObject.parseObject(match.getOverallRecord());
                    int win = json.getInteger("win");
                    int draw = json.getInteger("draw");
                    int lose = json.getInteger("lose");
                    match.setOverallRecord(String.format("近%d次交战，%s%d胜%d平%d负", win + draw + lose, match.getHostTeam(), win, draw, lose));
                }
                if (StrUtils.isNotEmpty(match.getAvgOdds())) {
                    JSONObject jo = JSONObject.parseObject(match.getAvgOdds());
                    String win = jo.getString("win_per");
                    String draw = jo.getString("draw_per");
                    String lose = jo.getString("lose_per");
                    match.setAvgOdds(win + "," + draw + "," + lose);
                }
                //近期战绩
                if (StringUtils.isNotBlank(match.getRecentRecord())) {
                    List<Map> jsonMap = JSONObject.parseArray(match.getRecentRecord(), Map.class);
                    String recentRecord = "";
                    for (int i = 0; i < jsonMap.size(); i++) {
                        Map json = jsonMap.get(i);
                        int win = (int) json.get("win");
                        int draw = (int) json.get("draw");
                        int lose = (int) json.get("lose");
                        if (i == 0)
                            recentRecord += String.format("主队%d胜%d平%d负", win, draw, lose);
                        if (i == 1)
                            recentRecord += String.format("，客队%d胜%d平%d负", win, draw, lose);
                    }
                    match.setRecentRecord(recentRecord);
                }
            }
        }
        matchApi.setCount(matchList.size());
        matchApi.setMatch(matchList);
        if ("1".equals(search.getMatchType()))
            matchApi.setLotteryId(LotteryIdEnum.JCLQ.getId());
        return matchApi;
    }

    @Override
    public List<HomeMatchVO> queryHomeHotMatchList() {
        List<HomeMatchVO> list = baseMapper.queryHomeHotMatchList();
        for(HomeMatchVO match:list){
            match.setMatchField(match.getMatchField().substring(2));
            Map map = JSONObject.parseObject(match.getOdds(), Map.class);
            Map _map = new HashMap();
            for(int i=0;i<6;i++){
                _map.put(i+"",map.get((i)+""));
            }
            match.setOdds(null);
            match.setOddsMap(_map);
        }
        return list;
    }

    @Override
    public List<HomeMatchVO> queryHomeHotMatchList2() {
        List<HomeMatchVO> list = baseMapper.queryHomeHotMatchList2();
        for(HomeMatchVO match:list){
            match.setMatchField(match.getMatchField().substring(2));
            Map map = JSONObject.parseObject(match.getOdds(), Map.class);
            Map _map = new HashMap();
            for(int i=0;i<6;i++){
                _map.put(i+"",map.get((i)+""));
            }
            match.setOdds(null);
            match.setOddsMap(_map);
            Integer count = baseMapper.searchPeopleCount(match.getCode());
            match.setPeopleJoin(String.valueOf(count+10));
        }
        return list;
    }

    @Override
    public JczqMatchPO getByCode(String code) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("code",code);
        wrapper.eq("match_type",0);
        JczqMatchPO po = this.selectOne(wrapper);
        return po;
    }

    @Override
    public List<HomeMatchVO> queryWhichActivity() {
        return baseMapper.queryHomeHotMatchList();
    }

    @Override
    public List<JczqMatchLiveVO> getNewsHomeMatch() {
        return matchLiveMapper.selectMatchList();
    }

    public static void main(String[] args) {
        System.out.println(String.format("近%d次交战，%s%d胜%d平%d负",1,"212",2,3,4));
    }

}
